script_enemy_main{

let effect1=0;
let object1=[];
let xscale1=0.01;
let yscale1=0.01;
let xscale2=0.01;
let yscale2=0.01;

let petal=191;
let size=1;
let maxspeed=1;
let vine=220;

let shot1=0;
let accel=0;
let movecounter=0;
let radius=0;
let scale=0;
let fade=220;

let shot2=0; let bullet2=[]; let timer2=[];
let shot3=0; let bullet3=[]; let timer3=[];
let shot4=0; let bullet4=[]; let timer4=[];
let shot5=0; let bullet5=[]; let timer5=[];

let playedse=0;
let usedbomb=0;

let character="Tomoko";
let cutin=character;
let dispelled=0;
let spellcards=2;
let spellcardnumber=40;
let damagerate=10;
let outfit=1;
outfit=(128*outfit)-128;
let usespell=0;
let bgfade=0;
let frame=0;
let time=0;
let miny=GetClipMinY; let maxy=GetClipMaxY; let minx=GetClipMinX; let maxx=GetClipMaxX;
let cx=GetCenterX; let cy=GetCenterY;

let SEmagics1=("\script\SoundEffects\magics1.wav");
let SEmagics5=("\script\SoundEffects\magics5.wav");
let SEshots4=("\script\SoundEffects\shots4.wav");
let SEshotm4=("\script\SoundEffects\shotm4.wav");

let EFwings=("\script\Images\OtherEffects\ButterflyWings.png");
let EFspellcircle=("\script\Images\OtherEffects\SpellCircle1b.png");

let BG1=("\script\Images\BackgroundLayers\Tomoko1.png");
let GRboss=("\script\Images\CharacterSprites\"~character~".png");

#include_function "script/Functions/SetSpellcardCommonData.txt";
#include_function "script/Functions/SpellcardNameLoad.txt";
#include_function "script/Functions/HealthBarLoad.txt";
#include_function "script/Functions/CutInLoad.txt";

@Initialize{
	LoadUserShotData("script\shots\Shots"~character~"1.txt");

	LoadSE("\script\SoundEffects\magics1.wav");
	LoadSE("\script\SoundEffects\magics5.wav");
	LoadSE("\script\SoundEffects\shots4.wav");
	LoadSE("\script\SoundEffects\shotm4.wav");

	LoadGraphic("\script\Images\CharacterSprites\"~character~".png");
	LoadGraphic("\script\Images\BackgroundLayers\Tomoko1.png");

	LoadGraphic("\script\Images\OtherEffects\ButterflyWings.png");
	LoadGraphic("\script\Images\OtherEffects\SpellCircle1b.png");

	SetScore(500000);
	SetLife(600);
	SetTimer(70);
	SetInvincibility(120);
	SetDamageRate(10,10);
	SetEnemyMarker(true);
	MagicCircle(true);
	#include_function "script/Functions/Focus.txt";
	Focus(character);
	SetEffectForZeroLife(60,100,1);
	SetMovePosition02(cx,miny+100,50);
}
	
@MainLoop{

SetCollisionA(GetX,GetY,16);
SetCollisionB(GetX,GetY,16);
SetShotAutoDeleteClip(32,32,32,32);

let weaken=1;
if(GetCommonData("BombOn")==0){ damagerate=10; }
if(GetCommonData("BombOn")==1){ damagerate=8; }
SetDamageRate(damagerate*weaken,damagerate*weaken);

let difficulty="";
if(GetCommonDataDefault("Difficulty",2)==1){ difficulty="Easy"; }
if(GetCommonDataDefault("Difficulty",2)==2){ difficulty="Normal"; }
if(GetCommonDataDefault("Difficulty",2)==3){ difficulty="Hard"; }
if(GetCommonDataDefault("Difficulty",2)==4){ difficulty="Lunatic"; }

SpellcardName("Biogenesis [Tree Of Life] ("~difficulty~")",spellcardnumber); 
HealthBar();
Portrait(cutin,3);

if(time%200==0 && time>=60){
	if(GetPlayerX>minx+50 && GetPlayerX<maxx-50){
	SetMovePosition01(GetPlayerX+rand(-30,30),rand(miny+80,miny+120),1.5);
	}
	if(GetPlayerX<=minx+50){
	SetMovePosition01(GetPlayerX+rand(15,30),rand(miny+80,miny+120),1.5);
	}
	if(GetPlayerX>=maxx-50){
	SetMovePosition01(GetPlayerX-rand(15,30),rand(miny+80,miny+120),1.5);
	}
}


if(time==60){
	loop(2){
	effect1=(Obj_Create(OBJ_EFFECT));
	Obj_SetPosition(effect1,GetX,GetY);
	ObjEffect_SetScale(effect1,xscale1,yscale1);
	ObjEffect_SetLayer(effect1,1); ObjEffect_SetTexture(effect1,EFwings); ObjEffect_SetRenderState(effect1,ADD);
	ObjEffect_SetPrimitiveType(effect1,PRIMITIVE_TRIANGLESTRIP); ObjEffect_CreateVertex(effect1,4);
	ObjEffect_SetVertexXY(effect1,0,-190,-100); ObjEffect_SetVertexUV(effect1,0,0,0);
	ObjEffect_SetVertexXY(effect1,1,190,-100); ObjEffect_SetVertexUV(effect1,1,380,0);
	ObjEffect_SetVertexXY(effect1,2,-190,100); ObjEffect_SetVertexUV(effect1,2,0,200);
	ObjEffect_SetVertexXY(effect1,3,190,100); ObjEffect_SetVertexUV(effect1,3,380,200);
	ObjEffect_SetVertexColor(effect1,0,180,255,255,255); ObjEffect_SetVertexColor(effect1,1,180,255,255,255);
	ObjEffect_SetVertexColor(effect1,2,180,255,255,255); ObjEffect_SetVertexColor(effect1,3,180,255,255,255);
	object1=object1~[effect1];
	}
}

if(time>=60){
	if(xscale1<1){ xscale1+=(xscale1*0.1); }
	if(yscale1<1){ yscale1+=(yscale1*0.2); }
	if(xscale2<1.1){ xscale2+=(xscale2*0.09); }
	if(yscale2<1.2){ yscale2+=(yscale2*0.18); }
	Obj_SetPosition(object1[0],GetX,GetY-30);
	ObjEffect_SetScale(object1[0],xscale1,yscale1);
	ObjEffect_SetVertexColor(object1[0],0,200,0,255,255); ObjEffect_SetVertexColor(object1[0],1,200,0,255,255);
	ObjEffect_SetVertexColor(object1[0],2,200,255,255,255); ObjEffect_SetVertexColor(object1[0],3,200,255,255,255);
	Obj_SetPosition(object1[1],GetX,GetY-30);
	ObjEffect_SetScale(object1[1],xscale2+0.1*cos(time*2),yscale2+0.2*cos(time*2));
	ObjEffect_SetVertexColor(object1[1],0,200,255,100,255); ObjEffect_SetVertexColor(object1[1],1,200,255,100,255);
	ObjEffect_SetVertexColor(object1[1],2,50,255,100,255); ObjEffect_SetVertexColor(object1[1],3,50,255,100,255);
}


if(frame>=300 && frame%80==0){
let anglex=rand(0,360);
	loop(30){
	if(frame%160==0){ CreateShot02(GetX,GetY,5,anglex,-0.1,1,178,10); }
	if(frame%160!=0){ CreateShot02(GetX,GetY,5,anglex,-0.1,1,170,10); }
	anglex+=360/30;
	}
PlaySE(SEshotm4);
}

if(OnBomb==true){ usedbomb=30; }
if(usedbomb>0){ usedbomb--; }
if(usedbomb==1){ DeleteEnemyShot(ALL); }
if(frame==880 || OnPlayerMissed==true || usedbomb==1){
movecounter=0; accel=0; radius=0; scale=0; fade=180; Obj_Delete(shot1); frame=60;
}

if(frame<180){ SetShotAutoDeleteClip(300,300,300,300); }
if(frame>=180){ SetShotAutoDeleteClip(32,32,32,32); }


if(frame==90 && GetTimer>1){
let xsize=200;
let ysize=200;
let playerx=GetPlayerX;
let playery=GetPlayerY;
let safedistance=70;
if(playerx<minx+safedistance){ playerx=minx+safedistance; } if(playerx>maxx-safedistance){ playerx=maxx-safedistance; }
if(playery<miny+safedistance){ playery=miny+safedistance; } if(playery>maxy-safedistance){ playery=maxy-safedistance; }
	shot1=(Obj_Create(OBJ_EFFECT));
	Obj_SetPosition(shot1,GetX,GetY); Obj_SetSpeed(shot1,0);
	Obj_SetAngle(shot1,atan2(playery-Obj_GetY(shot1),playerx-Obj_GetX(shot1)));
	Obj_SetAutoDelete(shot1,false); ObjEffect_SetScale(shot1,scale,scale);
	ObjEffect_SetLayer(shot1,1); ObjEffect_SetTexture(shot1,EFspellcircle); ObjEffect_SetRenderState(shot1,ADD);
	ObjEffect_SetPrimitiveType(shot1,PRIMITIVE_TRIANGLESTRIP); ObjEffect_CreateVertex(shot1,4);
	ObjEffect_SetVertexXY(shot1,0,-(xsize/2),-(ysize/2)); ObjEffect_SetVertexUV(shot1,0,0,0);
	ObjEffect_SetVertexXY(shot1,1,xsize/2,-(ysize/2)); ObjEffect_SetVertexUV(shot1,1,xsize,0);
	ObjEffect_SetVertexXY(shot1,2,-(xsize/2),ysize/2); ObjEffect_SetVertexUV(shot1,2,0,ysize);
	ObjEffect_SetVertexXY(shot1,3,xsize/2,ysize/2); ObjEffect_SetVertexUV(shot1,3,xsize,ysize);
	ObjEffect_SetVertexColor(shot1,0,fade,155,255,0); ObjEffect_SetVertexColor(shot1,1,fade,155,255,0);
	ObjEffect_SetVertexColor(shot1,2,fade,155,255,0); ObjEffect_SetVertexColor(shot1,3,fade,155,255,0);
	accel=0;
	radius=(((playerx-Obj_GetX(shot1))^2+(playery-Obj_GetY(shot1))^2)^0.5)/60*(pi/2);

let angle=rand(0,360);
let shota=0;
	loop(30){
	CreateShotA(shota,playerx+300*cos(angle),playery+300*sin(angle),15);
	SetShotDataA(shota,0,6.5,angle+180,0,-0.1,0,27);
	SetShotDataA(shota,60,0.2,angle+90,0,0,0,27);
	SetShotDataA(shota,210,0,angle,0,0.1,5,27);	
	FireShot(shota);
	angle+=360/60;
	CreateShotA(shota,playerx+300*cos(angle),playery+300*sin(angle),15);
	SetShotDataA(shota,0,6.5,angle+180,0,-0.1,0,27);
	SetShotDataA(shota,60,0.2,angle-90,0,0,0,27);
	SetShotDataA(shota,210,0,angle,0,0.1,5,27);	
	FireShot(shota);
	angle+=360/60;
	}
PlaySE(SEmagics5);
}

if(frame==150){
let shota=0;
	CreateShotA(shota,Obj_GetX(shot1),Obj_GetY(shot1),60);
	SetShotDataA(shota,0,0,0,0,0,0,4);
	SetShotKillTime(shota,0);	
	FireShot(shota);
}


if(frame>=90 && movecounter<60){
	accel+=180/60;
	scale+=1/60;
	Obj_SetSpeed(shot1,radius*sin(accel));
	ObjEffect_SetScale(shot1,scale,scale);
	ObjEffect_SetAngle(shot1,0,0,-time*10);
	movecounter++;
}
if(frame>=180){
	ObjEffect_SetVertexColor(shot1,0,fade,155,255,0); ObjEffect_SetVertexColor(shot1,1,fade,155,255,0);
	ObjEffect_SetVertexColor(shot1,2,fade,155,255,0); ObjEffect_SetVertexColor(shot1,3,fade,155,255,0);
if(fade>0){ fade-=3; }
}

//============================================

if(frame==240){
let angle=rand(0,360);
	loop(6){
	shot2=(Obj_Create(OBJ_SHOT));
	Obj_SetPosition(shot2,Obj_GetX(shot1),Obj_GetY(shot1));
	Obj_SetAngle(shot2,angle);
	Obj_SetSpeed(shot2,rand(1.5,2));
	ObjShot_SetGraphic(shot2,vine);
	bullet2=bullet2~[shot2];
	timer2=timer2~[shot2];
	timer2[length(bullet2)-1]=0;
	angle+=360/6;
	}
}

//============================================

let i=0;
	while(i<length(bullet2)){
		if(Obj_BeDeleted(bullet2[i])){
		bullet2=erase(bullet2,i); timer2=erase(timer2,i); 
		i--;
		}
		else{
		if(Obj_GetSpeed(bullet2[i])>0){ Obj_SetSpeed(bullet2[i],Obj_GetSpeed(bullet2[i])-0.015); }
		if(Obj_GetSpeed(bullet2[i])<=0){ Obj_SetSpeed(bullet2[i],0); }

if(Obj_GetSpeed(bullet2[i])>0 && time%12==0){
let shotx=0;
	CreateShotA(shotx,Obj_GetX(bullet2[i]),Obj_GetY(bullet2[i]),0);
	SetShotDataA(shotx,0,0,Obj_GetAngle(bullet2[i]),0,0,0,vine);
	SetShotKillTime(shotx,180);
	FireShot(shotx);
	PlaySE(SEshots4);
}

if(Obj_GetSpeed(bullet2[i])==0){
let angle=atan2(Obj_GetY(shot1)-Obj_GetY(bullet2[i]),Obj_GetX(shot1)-Obj_GetX(bullet2[i]));
	loop(8){
	shot3=(Obj_Create(OBJ_SHOT));
	Obj_SetPosition(shot3,Obj_GetX(bullet2[i]),Obj_GetY(bullet2[i]));
	Obj_SetAngle(shot3,angle+180+rand(-80,80));
	Obj_SetSpeed(shot3,rand(2,2.5));
	ObjShot_SetGraphic(shot3,vine);
	bullet3=bullet3~[shot3];
	timer3=timer3~[shot3];
	timer3[length(bullet3)-1]=0;
	}

let shotx=0;
let angle=rand(0,360);
	loop(5){
	CreateShotA(shotx,Obj_GetX(bullet2[i]),Obj_GetY(bullet2[i]),rand_int(0,5));
let color=rand(220,255);
let delay=rand_int(180,210);
	SetShotColor(color,color,color);
	SetShotDataA(shotx,0,-1,angle,0,0.04,0,petal);
	SetShotDataA(shotx,delay,0,angle,0,0.01,maxspeed,petal);
	SetShotColor(255,255,255);
	FireShot(shotx);
	if(OnPlayerMissed==false && GetEnemyShotCount>0 && playedse==0){ PlayDelayedSE(SEmagics1,180); playedse=60;}
	angle+=360/5;
	}
Obj_Delete(bullet2[i]);
PlaySE(SEmagics5);
}

	timer2[i]=timer2[i]+1;
	}
	i++;
}

//============================================

let i=0;
	while(i<length(bullet3)){
		if(Obj_BeDeleted(bullet3[i])){
		bullet3=erase(bullet3,i); timer3=erase(timer3,i); 
		i--;
		}
		else{
		if(Obj_GetSpeed(bullet3[i])>0){ Obj_SetSpeed(bullet3[i],Obj_GetSpeed(bullet3[i])-0.02); }
		if(Obj_GetSpeed(bullet3[i])<=0){ Obj_SetSpeed(bullet3[i],0); }

if(Obj_GetSpeed(bullet3[i])>0 && time%12==0){
let shotx=0;
	CreateShotA(shotx,Obj_GetX(bullet3[i]),Obj_GetY(bullet3[i]),0);
	SetShotDataA(shotx,0,0,Obj_GetAngle(bullet3[i]),0,0,0,vine);
	SetShotKillTime(shotx,180);
	FireShot(shotx);
	PlaySE(SEshots4);
}

if(Obj_GetSpeed(bullet3[i])==0){

let shotx=0;
let angle=rand(0,360);
	loop(5){
	CreateShotA(shotx,Obj_GetX(bullet3[i]),Obj_GetY(bullet3[i]),rand_int(0,5));
let color=rand(140,200);
let delay=rand_int(180,210);
	SetShotColor(255,255,color);
	SetShotDataA(shotx,0,-1,angle,0,0.04,0,petal);
	SetShotDataA(shotx,delay,0,angle,0,0.01,maxspeed,petal);
	SetShotColor(255,255,255);
	FireShot(shotx);
	if(OnPlayerMissed==false && GetEnemyShotCount>0 && playedse==0){ PlayDelayedSE(SEmagics1,180); playedse=60;}
	angle+=360/5;
	}
Obj_Delete(bullet3[i]);
PlaySE(SEmagics5);
}

	timer3[i]=timer3[i]+1;
	}
i++;
}

if(playedse>0){ playedse--; }


time++; frame++;
if(usespell>0){ usespell--; } if(usespell<0){ usespell++; }
SetCommonData("Boss1X",GetX); SetCommonData("Boss1Y",GetY);

#include_function "script/Functions/SpellcardName.txt";
#include_function "script/Functions/HealthBar.txt";
#include_function "script/Functions/CutIn.txt";

if(GetLife==0 && dispelled==0){ CreateEnemyFromFile("script\Functions\dispel.txt",GetX,GetY,0,0,character); dispelled=1; }

yield;
}

@BackGround{
	if(bgfade<255){ bgfade+=5; }

	SetTexture(BG1);
	SetAlpha(bgfade/2);
	SetGraphicScale(2,2);
	SetGraphicRect(0,0,350,350);
	SetGraphicAngle(0,0,-time/3);
	DrawGraphic(cx,cy-70);
}

@DrawLoop{
	SetGraphicScale(1,1);
	SetTexture(GRboss);
	SetGraphicAngle(0,0,0);
	SetColor(255,255,255);
	SetRenderState(ALPHA);

	if(usespell>=1){ SetGraphicRect(384,outfit,512,outfit+128); }
	if(usespell<=-1){ SetGraphicRect(512,outfit,640,outfit+128); }
	if(usespell==0){
		if(GetSpeedX<=0.5 && GetSpeedX>=-0.5){ SetGraphicRect(0,outfit,128,outfit+128); }
		else if(GetSpeedX<-0.5){ SetGraphicRect(128,outfit,256,outfit+128); }
		else if(GetSpeedX>0.5){ SetGraphicRect(256,outfit,384,outfit+128); }
	}
	DrawGraphic(GetX,GetY);
}

@Finalize{
//	SetCommonData("Conversation",1);
	NewPointData(spellcardnumber,GetCommonData("Difficulty"));
	#include_function "script/Functions/Main Menu/SpellcardDataAndPoints.txt";
}

task PlayDelayedSE(se,delay){
	loop(delay){ yield; }
	if(OnPlayerMissed==false && GetEnemyShotCount>0){ PlaySE(se); }
}

}